let requestUrl = null;

let isArray = ["否", "是"];
let relateArray = ["不关联", "关联"];
let relateTypes = ["不关联", "角色等级", "宠物等级"];

function addHandler() {
	let fabaoId = $("#fabao_id").combobox("getValue");

	if (fabaoId) {
		$("#fabaoId").combobox("setValue", fabaoId);
	}

	requestUrl = "/fabao_liangyi_attribute_category/insert";

	$("#liangyi_dialog").dialog("open");
}

function editHandler() {
	let rowData = $("#liangyi_list").datagrid("getSelected");
	
	if (rowData) {
		requestUrl = "/fabao_liangyi_attribute_category/updateById";

		$("#id").val(rowData.id);
		$("#note").textbox("setValue", rowData.note);
		$("#type").combobox("setValue", rowData.type);
		$("#max").numberspinner("setValue", rowData.max);
		$("#effect").textbox("setValue", rowData.effect);
		$("#fabaoId").combobox("setValue", rowData.fabaoId);
		$("#isPercent").combobox("setValue", rowData.isPercent);
		$("#propertyId").combobox("setValue", rowData.propertyId);
		$("#relateType").combobox("setValue", rowData.relateType);
		$("#relateGrade").combobox("setValue", rowData.relateGrade);

		$("#liangyi_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "warning");
	}
}

$(document).ready(function() {

	$("#fabao_id").combobox({
		url: "/fabao_category/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		panelHeight: "auto",
		prompt: "-法宝类型-",
		groupField: "type",
		groupFormatter: function (group) {
			ajaxGet("/fabao_type/selectById", {
				id: group
			}, function (resp) {
				let data = resp.data;

				$("#fabao_type" + group).html(data.name);
			}, error);

			return "<span class='blue' id='fabao_type" + group + "'></span>";
		},
		formatter: function(row) {
			return "<img height='20' src='" + row.image + "' />&nbsp;" + row.name;
		}
	});

	$("#search").linkbutton({
		iconCls: "icon-search"
	}).click(function() {
		let fabaoId = $("#fabao_id").combobox("getValue");

		$("#liangyi_list").datagrid("reload", {
			fabaoId: fabaoId
		});
	});

	$("#clear").linkbutton({
		iconCls: "icon-delete"
	}).click(function() {
		$("#search_form").form("clear");
	});

	/******************************************************************************************************/

	$("#type").combobox({
		url: "/liangyi_type/selectAll",
		valueField: "id",
		textField: "name",
		width: 120,
		required: true,
		panelHeight: "auto"
	});

	$("#fabaoId").combobox({
		url: "/fabao_category/selectAll",
		valueField: "id",
		textField: "name",
		width: 120,
		required: true,
		panelHeight: "auto",
		groupField: "type",
		groupFormatter: function (group) {
			ajaxGet("/fabao_type/selectById", {
				id: group
			}, function (resp) {
				let data = resp.data;

				$("#fabao_type_" + group).html(data.name);
			}, error);

			return "<span class='blue' id='fabao_type_" + group + "'></span>";
		},
		formatter: function(row) {
			return "<img width='20' src='" + row.image + "' />&nbsp;" + row.name;
		}
	});

	fetchSelect("#relateType", relateTypes, {
		width: 120,
		required: true
	});

	fetchSelect("#relateGrade", relateArray, {
		width: 120,
		required: true
	});

	$("#propertyId").combobox({
		url: "/property/selectAll",
		valueField: "id",
		textField: "name",
		width: 120,
		panelHeight: "auto"
	});

	$("#max").numberspinner({
		min: 1,
		width: 120,
		required: false
	});

	fetchSelect("#isPercent", isArray, {
		width: 120,
		required: true
	});

	$("#note").textbox({
		width: 320,
		height: 80,
		required: true,
		multiline: true
	});

	$("#effect").textbox({
		width: 320,
		height: 80,
		required: true,
		multiline: true
	});

	$("#liangyi_dialog").dialog({
		title: "法宝两仪",
		width: 440,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#liangyi_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(resp) {
						showMsg(resp.message);

						$(selector).form("clear");
						$("#liangyi_dialog").dialog("close");
						$("#liangyi_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#liangyi_form").form("clear");
				$("#liangyi_dialog").dialog("close");
			}
		}]
	});

	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr_with_image * 10;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_tool + heigt_header + heigt_body + heigt_footer + heigt_scoll_bar + 2;

	$("#liangyi_list").datagrid({
		url: "/fabao_liangyi_attribute_category/selectByPage",
		method: "get",
		height: height_table,
		pagination: true,
		rownumbers: true,
		fitColumns: false,
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler()
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}],
		frozenColumns: [[
			{field: "type", title: "两仪类型", align: "center", width: 100,
				formatter: function(value, rowData, rowIndex) {
					ajaxGet("/liangyi_type/selectById", {
						id: value
					}, function (resp) {
						let data = resp.data;

						$("#type_" + rowIndex).html(data.name);
					}, error);

					return "<div id='type_" + rowIndex + "'></div>";
				}
			},
			{field: "fabaoId", title: "法宝类型", align: "center", width: 100,
				formatter: function(value, rowData, rowIndex) {
					ajaxGet("/fabao_category/selectById", {
						id: value
					}, function (resp) {
						let data = resp.data;

						$("#fabao_" + rowIndex).attr({
							"title": data.name,
							"src": data.image,
							"alt": data.name
						});
					});

					return "<img height='" + size + "' id='fabao_" + rowIndex + "' />";
				}
			},
			{field: "note", title: "属性说明", align: "center", width: 400,
				formatter: function(value) {
					return "<div class='ell'>" + value + "</div>";
				}
			}
		]],
		columns: [[
			{field: "propertyId", title: "增加属性", align: "center", width: 100
				, formatter: function(value, rowData, rowIndex) {
					if (value) {
						ajaxGet("/property/selectById", {
							id: value
						}, function (resp) {
							let data = resp.data;

							$("#property_" + rowIndex).html(data.name);
						}, error);

						return "<div class='blue' id='property_" + rowIndex + "'></div>";
					}

					return "/";
				}
			},
			{field: "max", title: "最大属性", align: "center", width: 100},
			{field: "isPercent", title: "百分比表示", align: "center", width: 100,
				formatter: function(value) {
					return "<div class='blue'>" + isArray[value] + "</div>";
				}
			},
			{field: "relateType", title: "关联类型", align: "center", width: 100,
				formatter: function(value) {
					return "<div class='blue'>" + relateTypes[value] + "</div>";
				}
			},
			{field: "relateGrade", title: "关联等级", align: "center", width: 100,
				formatter: function(value) {
					return "<div class='blue'>" + relateArray[value] + "</div>";
				}
			},
			{field: "effect", title: "两仪效果", align: "center", width: 400,
				formatter: function(value) {
					return "<div class='ell'>" + value + "</div>";
				}
			}
		]]
	});

});